<template>
  <div class="header-wrapper">
    <div class="header-placeholder" :style="{ height: headerHeight + 'px' }" />
    <div class="header-fixed">
      <div class="header-content">
        <div class="header-logo hidden-xs-only">
          <img src="../../assets/images/logo.png" alt="">
        </div>
        <div class="header-logo-mini hidden-sm-and-up">
          <img src="../../assets/images/logo-mini.png" alt="">
        </div>
        <div class="header-tel hidden-xs-only">
          <div class="b1">全国400服务热线</div>
          <div class="b2">400-888-8888</div>
        </div>
        <ul class="header-menu">
          <li v-for="item in navMenu" :key="item.name" class="header-menu__item">
            <el-dropdown
              v-if="item.children && item.children.length"
              placement="bottom"
              trigger="click"
              @command="menuClick"
            >
              <span :class="['menu-span', routePath.includes(item.name) ? 'active' : '']">
                {{ item.meta.title }}
                <i class="el-icon-arrow-down" />
              </span>
              <el-dropdown-menu slot="dropdown" class="nav-sub-menu">
                <el-dropdown-item
                  v-for="subMenu in item.children"
                  :key="subMenu.name"
                  :class="[routePath.includes(subMenu.name) ? 'active' : '']"
                  :command="subMenu"
                >{{ subMenu.meta.title }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <span v-else :class="['menu-span', routePath.includes(item.name) ? 'active' : '']" @click="menuClick(item)">
              {{ item.meta.title }}
            </span>
          </li>
        </ul>
        <div class="header-button">
          <div class="button"><el-button @click="$router.push({ name: 'Register' })">注册</el-button></div>
          <div class="button"><el-button type="primary" @click="$router.push({ name: 'LoginForAccount' })">登录</el-button></div>
          <!--        <div class="button"><el-button>退出</el-button></div>-->
          <!--        <div class="button"><el-button type="primary">个人中心</el-button></div>-->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { routes } from '@/router'

export default {
  name: 'ComHeader',
  components: {},
  mixins: [],
  props: {},
  data() {
    return {
      headerHeight: 0
    }
  },
  computed: {
    navMenu() {
      return routes.filter(e => !e.hidden)
    },
    // 当前路由名称
    routePath() {
      return this.$route.matched.map(e => e.name)
    }
  },
  watch: {},
  created() {
  },
  mounted() {
    this.getHeaderHeight()
    window.addEventListener('resize', this.getHeaderHeight)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.getHeaderHeight)
  },
  methods: {
    // 获取导航栏高度
    getHeaderHeight() {
      const headerDom = document.querySelector('.header-fixed')
      this.headerHeight = headerDom.offsetHeight
    },
    menuClick(menu) {
      this.$router.push({
        name: menu.name
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import "../../element-variables.scss";
.header-wrapper {
  .header-fixed {
    width: 100%;
    position: fixed;
    left: 0;
    right: 0;
    margin: 0 auto;
    z-index: 9;
    top: 0;
    box-shadow: 5px 3px 5px rgba(133, 207, 248, 0.3);
    background-color: #fff;
  }
  .header-placeholder {
    width: 100%;
    height: 0;
  }
  .header-content {
    margin: 0 auto;
    max-width: 1200px;
    display: flex;
    align-items: center;
    //height: 80px;
    flex-wrap: wrap;
    .header-logo {
      //width: 200px;
      img {
        display: block;
        height: 80px;
        width: auto;
      }
    }
    .header-logo-mini {
      //width: 80px;
      padding-left: 10px;
      img {
        display: block;
        height: 40px;
        width: auto;
      }
    }

    .header-tel {
      margin-left: 15px;

      .b1 {
        display: flex;
        align-items: center;
        justify-content: center;
        line-height: 15px;
        color: #212529;
        font-size: 13px;
        font-weight: 700;

        &::before {
          display: block;
          content: "";
          width: 15px;
          height: 15px;
          background: url("../../assets/images/phone-icon.png") no-repeat center;
          background-size: cover;
          margin-right: 5px;
        }
      }

      .b2 {
        color: #f33;
        font-size: 20px;
        font-weight: 700;
        margin: 5px 0 0;
      }
    }

    .header-menu {
      display: flex;
      flex: 1;
      flex-wrap: wrap;
      &__item {
        margin: 0 0 0 30px;
        //min-width: 36px;
        .menu-span {
          font-size: 18px;
          color: #333;
          cursor: pointer;
          &.active {
            color: $--color-primary;
          }
        }
      }
    }
    .header-button {
      display: flex;
      padding: 0 10px;
      .button:nth-child(1) {
        margin-right: 10px;
      }
    }
  }
}
.nav-sub-menu.el-dropdown-menu--medium {
  .el-dropdown-menu__item {
    line-height: 36px;
    font-size: 16px;
    &.active {
      color: $--color-primary;
    }
  }
}
/* 小于768px xs */
@media (max-width: 767px) {
  .header-wrapper {
    .header-content {
      .header-menu {
        &__item {
          margin: 5px 0 5px 10px;
          //min-width: 36px;
          .menu-span {
            font-size: 14px;
            //color: #333;
            //cursor: pointer;
            //&.active {
            //  color: $--color-primary;
            //}
          }
        }
      }
      .header-button {
        display: block;
        padding: 0 8px;
        .button:nth-child(1) {
          margin: 8px 0 4px;
        }
        .button:nth-child(2) {
          margin :4px 0 8px;
        }
        ::v-deep .el-button--medium {
          padding: 6px 10px;
          font-size: 12px;
          border-radius: 4px;
        }
      }
    }
  }

  .nav-sub-menu.el-dropdown-menu--medium {
    .el-dropdown-menu__item {
      line-height: 28px;
      font-size: 14px;
    }
  }
}
</style>
